package com.lcb.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcb.blog.base.BaseApiService;
import com.lcb.blog.base.BaseResponse;
import com.lcb.blog.entity.Blog;
import com.lcb.blog.mapper.BlogMapper;
import com.lcb.blog.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;

/****
 * @Author:lcb
 * @Description:Blog业务层接口实现类
 * @Date: 2020-12-29 14:38:32
 *****/
@Api(tags = "BlogServiceImpl")
@RestController
@RequestMapping("/blog")
@CrossOrigin
public class BlogServiceImpl extends BaseApiService implements BlogService {

    @Autowired
    private BlogMapper blogMapper;

    /**
     * Blog条件+分页查询
     * @param blog 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @ApiOperation(value = "Blog条件查询",notes = "条件查询Blog方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse findBlogList(@RequestBody Blog blog, @PathVariable(value = "page") int page, @PathVariable(value = "size") int size){
        //分页
        PageHelper.startPage(page,size);

        PageInfo<Blog> pagelist =new PageInfo<Blog>(blogMapper.select(blog));

        return setResultSuccess(pagelist);
    }

    /**
     * 删除
     * @param ids
     */
    @ApiOperation(value = "Blog根据ID删除",notes = "根据ID删除Blog方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse deleteBlogById (@PathVariable(value = "ids") String ids){

        String[] split = ids.split(",");
        Example example = new Example(Blog.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(split));
        blogMapper.deleteByExample(example);
        return setResultSuccess("删除成功");
    }

    /**
     * 修改Blog
     * @param blog
     */
    @ApiOperation(value = "Blog根据ID修改",notes = "根据ID修改Blog方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse updateBlog(@RequestBody Blog blog){
        blogMapper.updateByPrimaryKeySelective(blog);
        return setResultSuccess("修改成功");
    }

    /**
     * 增加Blog
     * @param blog
     */
    @ApiOperation(value = "Blog添加",notes = "添加Blog方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse addBlog(@RequestBody Blog blog){
        blogMapper.insertSelective(blog);
        return setResultSuccess("添加成功");
    }

    /**
     * 根据ID查询Blog
     * @param id
     * @return
     */
    @ApiOperation(value = "Blog根据ID查询",notes = "根据ID查询Blog方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse findBlogById(@PathVariable(value = "id") Long id){
        return setResultSuccess( blogMapper.selectByPrimaryKey(id));
    }

    /**
     * 查询Blog全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Blog",notes = "查询Blog所有方法详情",tags = {"BlogServiceImpl"})
    @Override
    public BaseResponse findBlogAll() {
        return setResultSuccess(blogMapper.selectAll());
    }
}
